<template>
    <div>
        <div class="order-flag" :style="styles.orderFlag">
            <text  :style="styles.containerFirstArg">{{orderNumber}}</text>
        </div>
        <div class="container" :style="{'border-color':'black','border-width': '2px',...styles.container}">
            <div @click="getFirstArg" class="container-first" >
                <text :style="styles.containerFirstArg">{{firstArg?firstArg.name:'1音'}}</text>
            </div>
            <div @click="getLastArg" class="container-last">
                <text :style="styles.containerFirstArg">{{lastArg?lastArg.name:'2音'}}</text>
            </div>
        </div>
        <image class="container-del-btn" :style="styles.delBtn" :src="img.delBtn" @click="handleDelete"></image>
    </div>
</template>

<script>
    import {computeInterval, getInterval, getMusicFlagByXYValue} from "../../../util/compute";
    import {getImagePath} from "../../../util/path-util";
    import {getFormatHeightPX, toast} from "../../../util/common";

    export default {
        name: "music-interval-input",
        props: {
            uuid: {
                type: String,
                default: ''
            },
            orderNumber: {
                type: String,
                default: '-1'
            }
        },
        data() {
            return {
                firstArg: null,
                lastArg: null,
                img: {
                    delBtn: getImagePath('delete')
                },
                styles:{
                    container:{
                      height:getFormatHeightPX(60)
                    },
                    orderFlag:{
                    },
                    containerFirstArg: {
                        'font-size': getFormatHeightPX(26)
                    },
                    delBtn:{
                        height:getFormatHeightPX(55)
                    }
                }
            }
        },
        methods: {
            getFirstArg: function () {
                this.$emit('pushFirstArg', (firstArg, cb) => {
                    this.firstArg = getMusicFlagByXYValue(firstArg.xValue, firstArg.yValue);
                    let res = {
                        index: this.orderNumber,
                        first:  this.firstArg ,
                        uuid: this.uuid,
                        last: this.lastArg,
                        result: computeInterval(firstArg.xValue, firstArg.yValue, this.lastArg.xValue, this.lastArg.yValue)
                    }
                    if (this.isFinished()) {
                        this.$emit('onFinish', res)
                        cb(res);
                    }
                })
            },
            isFinished() {
                return this.firstArg && this.lastArg
            },
            getLastArg() {
                this.$emit('pushLastArg', (lastArg, cb) => {
                    this.lastArg = getMusicFlagByXYValue(lastArg.xValue, lastArg.yValue);
                    let res = {
                        index: this.orderNumber,
                        first: this.firstArg,
                        last:  this.lastArg,
                        uuid: this.uuid,
                        result: computeInterval(this.firstArg.xValue, this.firstArg.yValue, lastArg.xValue, lastArg.yValue)
                    }
                    if (this.isFinished()) {
                        this.$emit('onFinish', res)
                        cb(res);
                    }
                })

            },
            handleDelete() {
                this.$emit('onDelete');
            }
        }
    }
</script>

<style scoped>
    .container {
        margin-top: 10px;
        margin-bottom: 10px;
        align-self: center;
        width: 250px;
        /*height: 60px;*/
        margin-left: 10px;
        border-radius: 40px;
        border-style: solid;
        justify-content: center;
    }

    .order-flag {
        position: absolute;
        left: 10px;
        border-radius: 20px;
        border-style: solid;
        width: 50px;
        border-color: black;
        border-width: 1px;
        justify-content: center;
        align-items: center;
        top: 10px;
        bottom: 10px;
    }

    .container-first {
        position: absolute;
        left:25px;
        border-radius: 40px;
        border-style: solid;
        width: 90px;
        border-color: black;
        border-width: 1px;
        justify-content: center;
        align-items: center;
        top: 5px;
        bottom: 5px;
    }

    .container-del-btn {
        position: absolute;
        top: 14px;
        right: 5px;
        width: 50px;
        /*height: 50px;*/
    }

    .container-last {
        position: absolute;
        right: 25px;
        border-radius: 40px;
        border-style: solid;
        width:90px;
        justify-content: center;
        align-items: center;
        border-color: black;
        border-width: 1px;
        top: 5px;
        bottom: 5px;
    }
</style>
